class Typical < Formula
  desc "Data interchange with algebraic data types"
  homepage "https://github.com/stepchowfun/typical"
  url "https://github.com/stepchowfun/typical/archive/v0.9.6.tar.gz"
  sha256 "32d8a6b8ad8b7601fabf81f1bf66b4ac05e3f6f08e80ee59997409a43baac62b"
  license "MIT"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_ventura:  "9ff351c8d3357078bfb6d81f4057b346a029a1545d837116ab0e9c3cd7739485"
    sha256 cellar: :any_skip_relocation, arm64_monterey: "5b941e2fa63ab05cb68c49ab29917903fd600cf0745652fcaa8e3a63835b1c6d"
    sha256 cellar: :any_skip_relocation, arm64_big_sur:  "744cb98acceaf26c9e98e549f783e953e4b7e53bf02c1c4350c8b7057380c7c6"
    sha256 cellar: :any_skip_relocation, ventura:        "b6ca3b1cc3facb5309d7a8defc84d8c8851d5a133b472370623c50c853dae00e"
    sha256 cellar: :any_skip_relocation, monterey:       "faec7d53ef73a7e62deaca35d88a9f43bb2d853c20a5e7a10e85b80f57916bb7"
    sha256 cellar: :any_skip_relocation, big_sur:        "76e4e837167097c3782bcd0cf892cca6f89774c2bb5bdbac14daa612acd0d1c7"
    sha256 cellar: :any_skip_relocation, x86_64_linux:   "9f5b1d123494afb5779bff3e05272e4ec155a86baad5abafa4689e69675dd32d"
  end

  depends_on "rust" => :build

  def install
    system "cargo", "install", *std_cargo_args
  end

  test do
    (testpath/"types.t").write <<~EOS
      struct SendEmailRequest {
          to: String = 0
          subject: String = 1
          body: String = 2
      }

      choice SendEmailResponse {
          success = 0
          error: String = 1
      }
    EOS

    assert_empty shell_output("#{bin}/typical generate types.t --rust types.rs --typescript types.ts")

    generated_rust_code = (testpath/"types.rs").read
    generated_typescript_code = (testpath/"types.ts").read

    assert_match "// This file was automatically generated by Typical", generated_rust_code
    assert_match "pub struct SendEmailRequestIn", generated_rust_code
    assert_match "pub struct SendEmailRequestOut", generated_rust_code
    assert_match "pub enum SendEmailResponseIn", generated_rust_code
    assert_match "pub enum SendEmailResponseOut", generated_rust_code
    assert_match "// This file was automatically generated by Typical", generated_typescript_code
    assert_match "export type SendEmailRequestIn", generated_typescript_code
    assert_match "export type SendEmailRequestOut", generated_typescript_code
    assert_match "export type SendEmailResponseIn", generated_typescript_code
    assert_match "export type SendEmailResponseOut", generated_typescript_code
  end
end
